local skel = fk.CreateSkill {
  name = "emo__moyin",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__moyin"] = "魔音",
  [":emo__moyin"] = "锁定技，每当你失去仅一张牌时，若比前两次的点数：均大或均小，你摸一张牌；在两牌点数之间，你须将一张牌当【杀】使用；存在相同点数，你回复一点体力。",

  ["@emo__moyin"] = "魔音",
  ["#emo__moyin-slash"] = "魔音：你须将一张牌当【杀】使用",
}

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(skel.name) then return end
    local record = ((data.extra_data or Util.DummyTable).emo__moyin or  Util.DummyTable)[player.id]
    if record == "slash" then
      return not player:isNude()
    elseif record == "recover" then
      return player:isWounded()
    else
      return record ~= nil
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if data.extra_data.emo__moyin[player.id] == "draw" then
      player:drawCards(1, skel.name)
    elseif data.extra_data.emo__moyin[player.id] == "recover" then
      room:recover { num = 1, skillName = skel.name, who = player, recoverBy = player }
    elseif not player:isNude() then
      local use_data = {bypass_times = true}
      room:askToUseVirtualCard(player, {
        name = "slash", skill_name = skel.name, cancelable = false, prompt = "#emo__moyin-slash",
        extra_data = use_data,
        card_filter = { n = {1, 1}, pattern = "." }
      })
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skel.name, true)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local ids = {}
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if (info.fromArea == Card.PlayerEquip) or (info.fromArea == Card.PlayerHand) then
            table.insert(ids, info.cardId)
          end
        end
      end
    end
    if #ids ~= 1 then return end
    local new_num = Fk:getCardById(ids[1]).number
    local mark = player:getTableMark("@emo__moyin")
    table.insert(mark, new_num)
    if #mark > 3 then table.remove(mark, 1) end
    room:setPlayerMark(player, "@emo__moyin", mark)
    if #mark == 3 then
      data.extra_data = data.extra_data or {}
      data.extra_data.emo__moyin = data.extra_data.emo__moyin or {}
      if (new_num > mark[1] and new_num > mark[2]) or (new_num < mark[1] and new_num < mark[2]) then
        data.extra_data.emo__moyin[player.id] = "draw"
      elseif new_num == mark[1] or new_num == mark[2] then
        data.extra_data.emo__moyin[player.id] = "recover"
      elseif (new_num > mark[1] and new_num < mark[2]) or (new_num < mark[1] and new_num > mark[2]) then
        data.extra_data.emo__moyin[player.id] = "slash"
      end
    end
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  local mark = {}
  player.room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
    local ids = {}
    for _, move in ipairs(e.data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if (info.fromArea == Card.PlayerEquip) or (info.fromArea == Card.PlayerHand) then
            table.insert(ids, info.cardId)
          end
        end
      end
    end
    if #ids == 1 then
      table.insert(mark, Fk:getCardById(ids[1]).number)
    end
    return #mark == 3
  end, 1)
  room:setPlayerMark(player, "@emo__moyin", #mark > 0 and table.reverse(mark) or 0)
end)

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@emo__moyin", 0)
end)

return skel
